package edu.hrbu.trade01.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.hrbu.trade01.annotation.MyLog;
import edu.hrbu.trade01.entity.OperLog;
import edu.hrbu.trade01.entity.User;
import edu.hrbu.trade01.service.OperLogService;
import edu.hrbu.trade01.util.R;
import edu.hrbu.trade01.util.TableR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static edu.hrbu.trade01.util.ConstUtil.LOG_TITLE_DEL;
import static edu.hrbu.trade01.util.ConstUtil.LOG_TYPE_SYS;

/**
 * @author 徐登宇
 */
@RestController
@RequestMapping("/log")
public class OperLogController {

    @Autowired
    private OperLogService operLogService;

    // 条件分页查询
    @RequestMapping("/list")
    public TableR list(@RequestParam(value = "page", defaultValue = "1") int page,
                       @RequestParam(value = "limit", defaultValue = "5") int limit,
                       @RequestParam(value = "searchParams", required = false) String logStr) {
        Page<OperLog> pageModel = operLogService.getPageWithCondition(page, limit, logStr);
        return TableR.ok(pageModel.getTotal(), pageModel.getRecords());
    }

//    @MyLog(title = LOG_TITLE_DEL, businessType = LOG_TYPE_SYS)
    @PostMapping("/remove")
    public R remove(@RequestParam("id") Integer id){
        operLogService.removeById(id);
        return R.ok();
    }

    @PostMapping("/removeBatch")
    public R removeBatch(@RequestParam("ids") List<Integer> ids){
        operLogService.removeBatchByIds(ids);
        return R.ok();
    }

}
